package com.invoicerecognition.backend.service.impl.teacher;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.invoicerecognition.backend.mapper.StudentMapper;
import com.invoicerecognition.backend.mapper.TeacherMapper;
import com.invoicerecognition.backend.model.dto.Student;
import com.invoicerecognition.backend.model.dto.Teacher;
import com.invoicerecognition.backend.model.dto.User;
import com.invoicerecognition.backend.service.impl.utils.UserDetailsImpl;
import com.invoicerecognition.backend.service.teacher.AddStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AddStudentServiceImpl implements AddStudentService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Map<String, String> addStudent(Map<String, String> data) {

        String selectedOption = data.get("selectedOption");
        String searchQuery = data.get("searchQuery");

        Map<String, String> resp = new HashMap<>();

        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        int userId = user.getId();

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);

        if (teacher == null) {
            resp.put("error_message", "请先添加个人信息");
            return resp;
        }
        else {
            if ("选择添加方式".equals(selectedOption)) {
                resp.put("error_message", "请选择添加方式");
                return resp;
            }

            if ("grade".equals(selectedOption)) {
                if (searchQuery.isEmpty()) {
                    resp.put("error_message", "班级不能为空");
                    return resp;
                }

                QueryWrapper<Student> queryWrapperStudent = new QueryWrapper<>();
                queryWrapperStudent.eq("grade", searchQuery);
                List<Student> studentList = studentMapper.selectList(queryWrapperStudent);

                if (studentList.isEmpty()) {
                    resp.put("error_message", "该班级不存在");
                    return resp;
                }
                else {
                    Set<String> students = teacher.getStudents();
                    for (Student student: studentList) {
                        Integer studentUserId = student.getUserId();
                        students.add(studentUserId.toString());
                    }

                    teacherMapper.updateById(teacher);
                    resp.put("error_message", "success");
                }
            }
            else {
                if (searchQuery.isEmpty()) {
                    resp.put("error_message", "学号不能为空");
                    return resp;
                }

                QueryWrapper<Student> queryWrapperStudent = new QueryWrapper<>();
                queryWrapperStudent.eq("student_id", searchQuery);
                Student student = studentMapper.selectOne(queryWrapperStudent);
                if (student == null) {
                    resp.put("error_message", "该学生不存在");
                    return resp;
                }
                else {
                    Integer studentUserId = student.getUserId();

                    Set<String> students = teacher.getStudents();
                    students.add(studentUserId.toString());
                    teacher.setStudents(students);
                    teacherMapper.updateById(teacher);
                    resp.put("error_message", "success");
                }
            }

            return resp;
        }
    }
}
